//
//  FucSelectView.m
//  乐清
//
//  Created by mac on 16/5/4.
//  Copyright © 2016年 Simon. All rights reserved.
//

#import "FucSelectView.h"
#import "FucSelectViewController.h"
#import "RegisterViewController.h"

@implementation FucSelectView
{
//    NSArray *_colorArr;
    NSArray *_imgArr;
    NSArray *_titleArr;
    NSDictionary *_buttonsDic;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self _precastData];
    }
    return self;
}

- (void)setViewType:(NSInteger)viewType {
    
    _viewType = viewType;
    [self _createHeader];
    [self _createButtons];
    
    for (int i = 0; i < 3; i++) {
        ((UILabel *)[self viewWithTag:(205 + i + _viewType * 10)]).text = _buttonsDic[[NSString stringWithFormat:@"%ld", (long)_viewType]][i];
    }
    _titleLabel.text = _titleArr[_viewType - 1];
    
    if (_viewType > 1) {
        _imgView.center = CGPointMake(_imgView.center.x, _imgView.center.y + floorf(kScreenHeight / 24));
        _titleLabel.center = CGPointMake(_titleLabel.center.x, _titleLabel.center.y + floorf(kScreenHeight / 24));
    }
}

- (void)_precastData {
    
    _imgArr = @[[UIImage imageNamed:@"logo2"],[UIImage imageNamed:@"primary_2"],[UIImage imageNamed:@"junior_2"],[UIImage imageNamed:@"letter"],[UIImage imageNamed:@"primary_1"],[UIImage imageNamed:@"junior_1"],[UIImage imageNamed:@"public"],[UIImage imageNamed:@"private"],[UIImage imageNamed:@"check"]];
    _titleArr = @[@"乐清报名录取系统",@"小学新生入学预登记",@"初中新生入学预登记"];
    NSArray *firstButtonsArr = @[@"给家长的一封信",@"小学新生入学预登记",@"初中新生入学预登记"];
    NSArray *secondButtonArr = @[@"公办学校报名预登记",@"民办学校报名预登记",@"信息核对"];
    _buttonsDic = @{
                    @"1" : firstButtonsArr,
                    @"2" : secondButtonArr,
                    @"3" : secondButtonArr
                    };
}

- (void)_createHeader {
    
    UIColor *bgColor = [UIColor colorWithRed:54 / 255.0 green:207 / 255.0 blue:151 / 255.0 alpha:1];
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight / 3)];
    header.backgroundColor = bgColor;
    [self addSubview:header];
   
    CGFloat imgHeight = kScreenHeight / 12;
    int textFont = floorf(kScreenHeight / 24);
    CGFloat spaceHeight = header.height - imgHeight * 1.5;
    
    _imgView = [[UIImageView alloc] initWithFrame:CGRectMake((kScreenWidth - imgHeight) / 2, spaceHeight / 2, imgHeight, imgHeight)];
    _imgView.image = _imgArr[_viewType - 1];
//    _imgView.layer.cornerRadius = imgHeight / 2;
    [header addSubview:_imgView];
    
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, _imgView.bottom + spaceHeight / 10, kScreenWidth, textFont + 2)];
    _titleLabel.backgroundColor = bgColor;
    _titleLabel.font = [UIFont systemFontOfSize:textFont];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.textColor = [UIColor blackColor];
    [header addSubview:_titleLabel];
}

- (void)_createButtons {
    
    int buttonWidth = floorf((kScreenWidth - kSpaceToSideCustom * 2) / 9);
    int textFont = floorf(buttonWidth / 3);
    
    for (int i = 0; i < 3; i++) {
        UIButton *tapButton = [[UIButton alloc] initWithFrame:CGRectMake(kSpaceToSideCustom + buttonWidth + i * buttonWidth * 3, kScreenHeight / 3 + buttonWidth, buttonWidth, buttonWidth)];
//        tapButton.backgroundColor = _colorArr[i];
        if (_viewType == 1) {
            [tapButton setImage:_imgArr[i + 3] forState:UIControlStateNormal];
        } else {
            [tapButton setImage:_imgArr[i + 6] forState:UIControlStateNormal];
        }
        
        [tapButton addTarget:self action:@selector(tapButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        tapButton.tag = 200 + i + _viewType * 10;
        [self addSubview:tapButton];
        UILabel *buttonName = [[UILabel alloc] initWithFrame:CGRectMake(kSpaceToSideCustom + i * buttonWidth * 3, kScreenHeight / 3 + buttonWidth * 2.5, buttonWidth * 3, textFont + 2)];
        buttonName.font = [UIFont systemFontOfSize:textFont];
        buttonName.textAlignment = NSTextAlignmentCenter;
        buttonName.textColor = [UIColor blackColor];
        buttonName.tag = 200 + i + 5 + _viewType * 10;
        [self addSubview:buttonName];
    }

}

- (void)tapButtonAction:(UIButton *)sender {
    if (sender.tag == 210) {
        
    }
    if ((sender.tag == 211)||(sender.tag == 212)) {
        FucSelectViewController *fucSelectVC = [[FucSelectViewController alloc] init];
        fucSelectVC.viewCtype = sender.tag - 209;
        [self.viewController.navigationController pushViewController:fucSelectVC animated:YES];
    }
    if ((sender.tag == 220)||(sender.tag == 221)||(sender.tag == 230)||(sender.tag == 231)) {
        RegisterViewController *registerVC = [[RegisterViewController alloc] init];
        registerVC.viewCtype = sender.tag - 219;
        [self.viewController.navigationController pushViewController:registerVC animated:YES];
    }
    
}

@end
